package com.ningmengs789.live.live.live.common.widget.red;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import com.ningmengs789.live.R;
import com.ningmengs789.live.http.JsonBuilder;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.Interface.RequestCallback;
import com.ningmengs789.live.live.live.common.widget.gift.AbsDialogFragment;
import com.ningmengs789.live.live.live.common.widget.gift.view.DrawableTextView;
import com.ningmengs789.live.net.utils.NToast;

import org.json.JSONException;

public class SendRedPacketDialogFragment extends AbsDialogFragment implements View.OnClickListener {

    private String mLiveId;
    private String zbUserId;
    private DrawableTextView srpChoose1;
    private DrawableTextView srpChoose2;
    private int redPacketType = 1;//1 拼手气红包 2 平均红包

    private EditText srpEditText1;
    private EditText srpEditText2;
    private EditText srpEditText3;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_send_redpacket;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.BottomDialog2;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        mLiveId = bundle.getString("mLiveId");
        zbUserId = bundle.getString("zbUserId");

        srpChoose1 = mRootView.findViewById(R.id.srp_choose_1);
        srpChoose2 = mRootView.findViewById(R.id.srp_choose_2);
        srpChoose1.setOnClickListener(this);
        srpChoose2.setOnClickListener(this);

        mRootView.findViewById(R.id.srp_send).setOnClickListener(this);

        srpEditText1 = mRootView.findViewById(R.id.srp_editText_1);
        srpEditText2 = mRootView.findViewById(R.id.srp_editText_2);
        srpEditText3 = mRootView.findViewById(R.id.srp_editText_3);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.srp_choose_1:
                if (redPacketType == 1){
                    return;
                }
                redPacketType = 1;
                srpChoose1.setLeftDrawable(mContext.getResources().getDrawable(R.drawable.icon_srp_choose));
                srpChoose2.setLeftDrawable(mContext.getResources().getDrawable(R.drawable.icon_srp_no_choose));
                break;
            case R.id.srp_choose_2:
                if (redPacketType == 2){
                    return;
                }
                redPacketType = 2;
                srpChoose1.setLeftDrawable(mContext.getResources().getDrawable(R.drawable.icon_srp_no_choose));
                srpChoose2.setLeftDrawable(mContext.getResources().getDrawable(R.drawable.icon_srp_choose));
                break;
            case R.id.srp_send:
                loadData();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void loadData() {
        String bonus_money = srpEditText1.getText().toString();
        if (TextUtils.isEmpty(bonus_money)){
            NToast.shortToast(mContext, "请输入金额");
            return;
        }
        String bonus_num = srpEditText2.getText().toString();
        if (TextUtils.isEmpty(bonus_num)){
            NToast.shortToast(mContext, "请输入红包数量");
            return;
        }
        String bonus_tips = srpEditText3.getText().toString();
        if (TextUtils.isEmpty(bonus_tips)){
            bonus_tips = "恭喜发财，大吉大利";
        }
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("fl_uid", zbUserId)
                    .put("bonus_money", bonus_money)//发红包总音浪
                    .put("bonus_num", bonus_num)//发红包总个数
                    .put("bonus_type", redPacketType)
                    .put("bonus_tips", bonus_tips)//红包提示语
                    .put("live_id", mLiveId)
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/appbonus/RandomMoney", body, new RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                NToast.shortToast(mContext, errInfo);
            }
            @Override
            public void onSuccess(String result) {
                NToast.shortToast(mContext, "红包发布成功");
                dismiss();
            }
        });
    }
}
